#!/usr/bin/perl
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
#  
#  
# Licensed Materials - Property of IBM 
#  
# (C) COPYRIGHT International Business Machines Corp. 2000,2002 
# All Rights Reserved 
#  
# US Government Users Restricted Rights - Use, duplication or 
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp. 
#  
# IBM_PROLOG_END_TAG 
# @(#)80   1.28.1.79   src/csm/install/installms.perl, setup, csm_rpyxh, rpyxht1f3 9/23/02 13:23:56

#####################################################################
# Subroutines:                                                      #
#                                                                   #
#                                                                   #
#     usage run_cmd getArgs checkPreviousInstallation copyRpm       #
#     copy_csm copy_opensrc_prereqs copy_csmbin copy_distro_rpms    #
#     create_directory_structure install_rpm                        #
#     install_installp_packages 			            #
#     install_packages checkrpm set_variables                       # 
#                                                                   #
#                                                                   #
#                                                                   #
#####################################################################

use strict;
BEGIN { $::csmpm = $ENV{'CSM_PM'} ? $ENV{'CSM_PM'} : '/opt/csm/pm'; }
use lib $::csmpm;
use File::Path;	
# Provides mkpath();
use FindBin qw($Bin);
use lib "$Bin";

use Getopt::Std;
use NodeUtils;
use InstallDefs;
use InstallUtils;

$::MSGCAT = 'csmInstall.cat';
$::MSGMAPPATH = $ENV{'CSM_MSGMAPS'} ? $ENV{'CSM_MSGMAPS'} : '/opt/csm/msgmaps';
$::MSGSET = 'csminstall';

$::MNTCDROM		= "/mnt/cdrom";
if($ENV{LANG} =~ /^(C|POSIX|en_US)/){ $::ENGLISH=1; } #this is a work around for NLS problems

# PKGPATH is the list of directories from which the RPMs and Tarballs are
# to be copied.  The packages get copied into $RPMSOURCE and $TARBALLSOURCE
# and get installed from there.
# It is assumed that a CD-ROM is already mounted at /mnt/cdrom.
#$::DEFAULT_PKGPATH = "/opt/csm/install/distrib";
$::DEFAULT_PKGPATH = "/mnt/cdrom";
#$::PKGPATH = "";

@::VALID_ATTRS		= ( "RemoteShell","SetupRemoteShell" );


$::PREREQS_ATTR{'OSName'} = InstallUtils->get_OSName;   #AIX/Linux
$::PREREQS_ATTR{'DistributionVersion'} = InstallUtils->get_DistributionVersion; #5.1.0/7.1/7.2
$::PREREQS_ATTR{'PkgArchitecture'} = InstallUtils->get_PkgArchitecture; #i?86
$::PREREQS_ATTR{'DistributionName'} = InstallUtils->get_DistributionName; #RedHat
if ( $::PREREQS_ATTR{'PkgArchitecture'} =~ /i?86/)
{
   $::PREREQS_ATTR{'PkgArchitecture'}="i386";
}
$::PREREQS_ATTR{'CsmCoreVersion'} = InstallUtils->get_CSMVersion("csm\.core");
if (length($::PREREQS_ATTR{'CsmCoreVersion'})==0)
{
  NodeUtils->message('E2', 'EMsgNO_CORE');
}


sub set_variables {
if ($::PREREQS_ATTR{'OSName'} eq "AIX")
{
    my $temp_distro = $::PREREQS_ATTR{'OSName'}.$::PREREQS_ATTR{'DistributionVersion'};
    if (!grep(/^$temp_distro$/,@::VALID_AIX_DISTROS))
    {
          NodeUtils->message('E5', 'EMsgUNSUPPORTED_AIX_DISTRO', $temp_distro, join(', ',@::VALID_AIX_DISTROS));
    }
    else { NodeUtils->message('V', 'IMsgDETECTED_AIX_DISTRO', $temp_distro); }
    if (!grep(/^$::PREREQS_ATTR{'CsmCoreVersion'}$/,@::VALID_CSM_DISTROS))
    {
          NodeUtils->message('E5', 'EMsgUNSUPPORTED_CSM_DISTRO', "CSM".$::PREREQS_ATTR{'CsmCoreVersion'}, join(', ',@::VALID_CSM_DISTROS));
    }
    else { NodeUtils->message('V', 'IMsgDETECTED_CSM_DISTRO', "CSM".$::PREREQS_ATTR{'CsmCoreVersion'}); }

}
elsif ($::PREREQS_ATTR{'OSName'} eq "Linux")
{
   my $temp_distro = $::PREREQS_ATTR{'DistributionName'}. " " .$::PREREQS_ATTR{'DistributionVersion'};
   if (!grep(/^$temp_distro$/, @::VALID_DISTROS))
   {
        NodeUtils->message('E5', 'EMsgUNSUPPORTED_DISTRO', $temp_distro, join(', ',@::VALID_DISTROS));
   }
   else { NodeUtils->message('V', 'IMsgDETECTED_DISTRO', $temp_distro); }
   if (!grep(/^$::PREREQS_ATTR{'CsmCoreVersion'}$/,@::VALID_CSM_DISTROS))
   {
         NodeUtils->message('E5', 'EMsgUNSUPPORTED_CSM_DISTRO', "CSM".$::PREREQS_ATTR{'CsmCoreVersion'}, join(', ',@::VALID_CSM_DISTROS));
   }
   else { NodeUtils->message('V', 'IMsgDETECTED_CSM_DISTRO', "CSM".$::PREREQS_ATTR{'CsmCoreVersion'}); }
}
else
{
     NodeUtils->message("E2", 'EMsgINVALID_OSTYPE');
}


if ($::PREREQS_ATTR{'OSName'} eq "AIX")
{
   $::PREREQS_ATTR{'OSTypeVer'} =  $::PREREQS_ATTR{'OSName'}.$::PREREQS_ATTR{'DistributionVersion'};
   $::INSTALLDIR           ="/csminstall";
   $::INSTALLDIR_CSM       =$::INSTALLDIR."/csm";                                  #/csminstall/csm
   $::INSTALLDIR_CSMVR     =$::INSTALLDIR_CSM."/".$::PREREQS_ATTR{CsmCoreVersion}; #/csminstall/csm/1.2/
   $::INSTALLDIR_CSMVRBIN  =$::INSTALLDIR_CSMVR."/bin";                                 #/csminstall/csm/1.2/bin

   $::INSTALLDIR_OS        =$::INSTALLDIR."/".$::PREREQS_ATTR{"OSName"};           #/csminstall/AIX 
   $::INSTALLDIR_OSVER     =$::INSTALLDIR_OS."/".$::PREREQS_ATTR{DistributionVersion};        #/csminstall/AIX/5.1
   $::INSTALLDIR_OSCSM     =$::INSTALLDIR_OS."/csm";                               #/csminstall/AIX/csm

   $::INSTALLDIR_CSMVER    =$::INSTALLDIR_OSCSM."/".$::PREREQS_ATTR{CsmCoreVersion}; #/csminstall/AIX/csm/1.2.0

   $::INSTALLDIR_CSMBIN    =$::INSTALLDIR_CSMVER."/bin"; #/csminstall/AIX/csm/1.2.0/bin
   $::INSTALLDIR_CSMPKG    =$::INSTALLDIR_CSMVER."/packages"; #/csminstall/AIX/csm/1.2.0/packages
   $::INSTALLDIR_CSMRPM    =$::INSTALLDIR_CSMPKG."/rpms"; #/csminstall/AIX/csm/1.2.0/packages/rpms
   $::INSTALLDIR_CSMINSTLP =$::INSTALLDIR_CSMPKG."/installp"; #/csminstall/AIX/csm/1.2.0/packages/installp

   @::server_open_srcrpms =(@{$::opensrc_prereqs{'csm.server'}{$::PREREQS_ATTR{'OSTypeVer'}}{$::PREREQS_ATTR{'CsmCoreVersion'}}});
   @::client_open_srcrpms =(@{$::opensrc_prereqs{'csm.client'}{$::PREREQS_ATTR{'OSTypeVer'}}{$::PREREQS_ATTR{'CsmCoreVersion'}}});
   @::core_open_srcrpms =(@{$::opensrc_prereqs{'csm.core'}{$::PREREQS_ATTR{'OSTypeVer'}}{$::PREREQS_ATTR{'CsmCoreVersion'}}});
   @::opensrc_rpm_prereqs =(@::core_open_srcrpms, @::server_open_srcrpms, @::client_open_srcrpms);
   @::csm_server_packages =(@{$::csm_packages{'MgmtSvr'}{$::PREREQS_ATTR{'OSTypeVer'}}{$::PREREQS_ATTR{'CsmCoreVersion'}}});
   @::csm_client_packages =(@{$::csm_packages{'MgdNode'}{$::PREREQS_ATTR{'OSTypeVer'}}{$::PREREQS_ATTR{'CsmCoreVersion'}}});
   @::csm_packages =(@::csm_server_packages ,@::csm_client_packages);
   @::csm_packages =InstallUtils->get_uniqu_arry_elemnts(@::csm_packages);
   @::rsct_server_packages =(@{$::rsct_packages{'MgmtSvr'}{$::PREREQS_ATTR{'OSTypeVer'}}{$::PREREQS_ATTR{'CsmCoreVersion'}}});
   @::rsct_client_packages =(@{$::rsct_packages{'MgdNode'}{$::PREREQS_ATTR{'OSTypeVer'}}{$::PREREQS_ATTR{'CsmCoreVersion'}}});
   @::rsct_packages =(@::rsct_server_packages ,@::rsct_client_packages);
   @::rsct_packages =InstallUtils->get_uniqu_arry_elemnts(@::rsct_packages);
   @::os_prereq =(@{$::Linux_os_prereqs{$::PREREQS_ATTR{'OSTypeVer'}}});
   @::csm_bin_file_csm =(@{$::csm_bin_copy_csm{$::PREREQS_ATTR{'OSTypeVer'}}{$::PREREQS_ATTR{'CsmCoreVersion'}}});
   @::csm_bin_file_tftpboot =(@{$::csm_bin_copy_tftpboot{$::PREREQS_ATTR{'OSTypeVer'}}{$::PREREQS_ATTR{'CsmCoreVersion'}}});
   @::csm_bin_file_csmver =(@{$::csm_bin_copy_csmver{$::PREREQS_ATTR{'OSTypeVer'}}{$::PREREQS_ATTR{'CsmCoreVersion'}}});

}
elsif ($::PREREQS_ATTR{'OSName'} eq "Linux")
{
   $::PREREQS_ATTR{'OSTypeVer'} =  $::PREREQS_ATTR{'OSName'}.$::PREREQS_ATTR{'DistributionName'}.$::PREREQS_ATTR{'DistributionVersion'};
   $::INSTALLDIR           ="/csminstall";
   $::INSTALLDIR_CSM       =$::INSTALLDIR."/csm";                                  #/csminstall/csm
   $::INSTALLDIR_CSMVR     =$::INSTALLDIR_CSM."/".$::PREREQS_ATTR{CsmCoreVersion}; #/csminstall/csm/1.2/
   $::INSTALLDIR_CSMVRBIN  =$::INSTALLDIR_CSMVR."/bin";                                 #/csminstall/csm/1.2/bin

   $::INSTALLDIR_OS        =$::INSTALLDIR."/".$::PREREQS_ATTR{"OSName"}."/".$::PREREQS_ATTR{'DistributionName'}; 
											#/csminstall/Linux/RedHat 
   $::INSTALLDIR_OSVER     =$::INSTALLDIR_OS."/".$::PREREQS_ATTR{DistributionVersion};  #/csminstall/Linux/RedHat/7.0
   $::INSTALLDIR_OSVER_ARCH=$::INSTALLDIR_OSVER."/".$::PREREQS_ATTR{'PkgArchitecture'}; #/csminstall/Linux/Redhat/7.0/i386

   $::INSTALLDIR_OSCSM     =$::INSTALLDIR_OS."/csm";                               #/csminstall/Linux/RedHat/csm
   $::INSTALLDIR_CSMVER    =$::INSTALLDIR_OSCSM."/".$::PREREQS_ATTR{CsmCoreVersion}; #/csminstall/Linux/RedHat/csm/1.2.0

   
   $::INSTALLDIR_OS_NAME  =$::INSTALLDIR_OSVER_ARCH."/".$::PREREQS_ATTR{DistributionName}; #/csminstall/Linux/Redhat/7.0/i386/RedHat

   $::INSTALLDIR_OS_NAME_RPMS  =$::INSTALLDIR_OS_NAME."/RPMS"; #/csminstall/Linux/Redhat/7.0/i386/RedHat/RPMS
   $::SMS_RPMS = $::INSTALLDIR_OSVER_ARCH."/RPMS"; #/csminstall/Linux/Redhat/7.0/i386/RPMS
   $::SMS_UPDATES = $::INSTALLDIR_OSVER_ARCH."/updates"; #/csminstall/Linux/Redhat/7.0/i386/updates
   #$::INSTALLDIR_OS_NAME_UPDATES  =$::INSTALLDIR_OS_NAME."/updates"; #/csminstall/Linux/Redhat/7.0/i386/RedHat/updates
   $::INSTALLDIR_CSMBIN    =$::INSTALLDIR_CSMVER."/bin"; #/csminstall/Linux/RedHat/csm/1.2.0/bin
   $::INSTALLDIR_CSMPKG    =$::INSTALLDIR_CSMVER."/packages"; #/csminstall/Linux/RedHat/csm/1.2.0/packages



   @::server_open_srcrpms =(@{$::opensrc_prereqs{'csm.server'}{$::PREREQS_ATTR{'OSTypeVer'}}{$::PREREQS_ATTR{'CsmCoreVersion'}}});
   @::client_open_srcrpms =(@{$::opensrc_prereqs{'csm.client'}{$::PREREQS_ATTR{'OSTypeVer'}}{$::PREREQS_ATTR{'CsmCoreVersion'}}});
   @::core_open_srcrpms =(@{$::opensrc_prereqs{'csm.core'}{$::PREREQS_ATTR{'OSTypeVer'}}{$::PREREQS_ATTR{'CsmCoreVersion'}}});
   @::director_open_srcrpms =(@{$::opensrc_prereqs{'csm.director.agent'}{$::PREREQS_ATTR{'OSTypeVer'}}{$::PREREQS_ATTR{'CsmCoreVersion'}}});
   @::opensrc_rpm_prereqs =(@::core_open_srcrpms, @::server_open_srcrpms, @::client_open_srcrpms, @::director_open_srcrpms);
   @::csm_server_packages =(@{$::csm_packages{'MgmtSvr'}{$::PREREQS_ATTR{'OSTypeVer'}}{$::PREREQS_ATTR{'CsmCoreVersion'}}});
   @::csm_client_packages =(@{$::csm_packages{'MgdNode'}{$::PREREQS_ATTR{'OSTypeVer'}}{$::PREREQS_ATTR{'CsmCoreVersion'}}});
   @::csm_packages =(@::csm_server_packages ,@::csm_client_packages);
   @::csm_packages =InstallUtils->get_uniqu_arry_elemnts(@::csm_packages);
   @::rsct_server_packages =(@{$::rsct_packages{'MgmtSvr'}{$::PREREQS_ATTR{'OSTypeVer'}}{$::PREREQS_ATTR{'CsmCoreVersion'}}});
   @::rsct_client_packages =(@{$::rsct_packages{'MgdNode'}{$::PREREQS_ATTR{'OSTypeVer'}}{$::PREREQS_ATTR{'CsmCoreVersion'}}});
   @::rsct_packages =(@::rsct_server_packages ,@::rsct_server_packages);
   @::rsct_packages =InstallUtils->get_uniqu_arry_elemnts(@::rsct_packages);
   @::director_packages =(@{$::director_packages{'MgmtSvr'}{$::PREREQS_ATTR{'OSTypeVer'}}{$::PREREQS_ATTR{'CsmCoreVersion'}}});
   @::os_prereq =(@{$::Linux_os_prereqs{$::PREREQS_ATTR{'OSTypeVer'}}});
   @::csm_bin_file_csm =(@{$::csm_bin_copy_csm{$::PREREQS_ATTR{'OSTypeVer'}}{$::PREREQS_ATTR{'CsmCoreVersion'}}});
   @::csm_bin_file_tftpboot =(@{$::csm_bin_copy_tftpboot{$::PREREQS_ATTR{'OSTypeVer'}}{$::PREREQS_ATTR{'CsmCoreVersion'}}});
   @::csm_bin_file_csmver =(@{$::csm_bin_copy_csmver{$::PREREQS_ATTR{'OSTypeVer'}}{$::PREREQS_ATTR{'CsmCoreVersion'}}});


}
else
{
     NodeUtils->message("E2", 'EMsgINVALID_OSTYPE');
}

}  # end of sub routine set_variables

sub usage
{

    NodeUtils->message('I', 'IMsginstallmsUsage',
					join (' ', @::VALID_ATTRS));
    exit 1;
}

# Run a system command.
#
# The first argument is a string containing the command to run
#
# The second argument is the type of message that should be printed.
#    Valid values for msgtype are:  E, W, I, etc.  (same as the msg routine)
#    If msgtype is "E" (the default), the command exits the program with the
#    return code of the command.
#
# The third argument specifies how the command will run.
#
#    If runtype is "show_errors" or not specified, stdout will be captured and 
#    displayed in a message as specified by msgtype.
# 
#    If runtype is "show_all", all output will go be sent to stdout
#    or wherever the command sends it.  Any errors will be displayed as a
#    message as specified by msgtype.
#
#    If runtype is "ignore_errors", all output will go be sent to the screen
#    or wherever the command sends it.  Any errors will be ignored.
#
sub run_cmd
{
    my ($cmd, $msgtype, $runtype) = @_;
    my ($rc, @cmdout, $msgtypestr);

    $msgtype = "E" if (!$msgtype);
    $runtype = "show_errors" if (!$runtype);

    NodeUtils->message('V','IMsgCMD',$cmd);
    if ($runtype eq "show_all")
    {
	$rc = system("$cmd") >> 8;
	if ($rc) 
	{ 
	    # Only exit with a return code if msgtype = "E" (Error)
	    $msgtypestr = $msgtype;
	    $msgtypestr = "$msgtype$rc" if ($msgtype eq "E");
	    NodeUtils->message("$msgtypestr", 'EMsgCMD_FAILED_RC', $cmd, $rc);
	}
    }
    elsif ($runtype eq "show_errors")
    {
	@cmdout = `$cmd 2>&1`;
        chomp (@cmdout); # take the newline off
	$rc = $? >> 8;	

	if ($rc) 
	{ 
	    # Only exit with a return code if msgtype = "E" (Error)
	    $msgtypestr = $msgtype;
	    $msgtypestr = "$msgtype$rc" if ($msgtype eq "E");

	    NodeUtils->message("$msgtypestr",'EMsgCMD_FAILED',$cmd,join('', @cmdout)); 
	}
    }
    elsif ($runtype eq "ignore_errors")
    {
	$rc = system("$cmd") >> 8;
    }
    else
    {
	NodeUtils->message('E1','EMsgRUN_CMD_INVALID', $runtype);
    }

    return $rc;
}


sub getArgs
{
    my $pkgpath = "";
    if (! getopts('Dp:fxvVh') ) { &usage; }

    if ($::opt_h) { &usage }
    $::FORCE_INSTALL = $::opt_f;
    if ($::opt_v || $::opt_V) 
    {
        if ($::opt_v) { $::VERBOSE = $::opt_v; }
        if ($::opt_V) { $::VERBOSE = $::opt_V; }

    }
    $::DONT_COPY_RPMS = $::opt_x;

    # Install the IBM Director Agent and Linux Cluster
    # Support Extension (Linux Only).
    $::INSTALL_DIRECTOR = $::opt_D;

    if ($::opt_x && $::opt_p) { &usage; }
    if (!($::opt_p ^ $::opt_x)) { &usage; }

    if ($::opt_p)
    {
	# Packages path
	$pkgpath = $::opt_p;
    }


    # Handle attr=value arguments
    if (scalar(@ARGV) > 0)     
    {
	foreach my $a (@ARGV)
	{
	    # Note:  we allow a null value
	    my ($attr, $value) = $a =~ /^\s*(\S+?)\s*=\s*(\S*.*)$/;
	    #print "ATTR = $attr, VALUE = $value\n";

	    # mkrsrc-api needs something for the value
	    if (!length($value)) 
	    { 
		$value = "''"; 
	    }

	    if (!defined($attr))
	    {
		NodeUtils->message('W', 'EMsgBAD_ATTR_VAL_FORM');
		&usage;
	    }
	    my $found = 0;
	    foreach my $va (@::VALID_ATTRS)
	    {
		if ($attr =~ /^$va$/i)
		{
		    # Put quotes around the value to protect spaces 
		    # or weird chars
		    #$::ATTRS{$va} = NodeUtils->quote($value);
		    $::ATTRS{$va} = $value;	# Quotes mess up some values
		    #print "ASSIGNED ATTR $va = VALUE $::ATTRS{$va}\n";
		    $found = 1;
		}
	    }
	    if (!$found)
	    {
		NodeUtils->message('E', 'EMsgBAD_ATTR', $attr);
		NodeUtils->message('I', 'IMsgVALID_ATTRS', 
					join ('\n\t', @::VALID_ATTRS));
		exit 1;
	    }
	}
    }

    #$pkgpath		= $::DEFAULT_PKGPATH	if (! $pkgpath);

    # Validate attributes and flags
    if (($::PREREQS_ATTR{'OSName'} ne "Linux") and $::INSTALL_DIRECTOR)
    {
	NodeUtils->message('E', 'EMsgLINUX_ONLY_FLAG', "-D");
	&usage;
    }
    if ($::PREREQS_ATTR{'OSName'} eq "Linux")
    {
	my $distro = $::PREREQS_ATTR{'DistributionName'}. " " .
		     $::PREREQS_ATTR{'DistributionVersion'};
	if (!grep(/^$distro$/, @::VALID_DISTROS))
	{
	    NodeUtils->message('E5', 'EMsgUNSUPPORTED_DISTRO', 
	                       $distro, join(', ',@::VALID_DISTROS));
	}
	else { NodeUtils->message('V', 'IMsgDETECTED_DISTRO', $distro); }
	$::DISTRO = $distro;
    }

    @::PKGPATH	= split(":", $pkgpath);
    foreach my $p (split(":", $pkgpath)){  #gather the subdirectories too
	    push @::PKGPATH, (InstallUtils->returnSubdirectory($p));
    }

}

# Check if csm is already installed.  If so, what happens?
sub checkPreviousInstallation
{
}

sub copyRpm
  {
    my ($rpmfile, $path, $destination) = @_;
    NodeUtils->message("I", 'IMsgCOPYING', $rpmfile);

    mkpath ($destination, $::VERBOSE, 0755);
    
    &run_cmd("$::COPY $path/$rpmfile $destination");
    &run_cmd("$::CHMOD +r $destination/$rpmfile");
    #If there are any errors during the copy or chmod, run_cmd() exits.
  }


# Copy all the RPM files from the CSM CD-ROM 
sub copy_csm
{
 if ($::PREREQS_ATTR{'OSName'} eq "Linux")
 {
    my ($rpmfile, $rpm, $path);
    NodeUtils->message("I", 'IMsgCOPYING_RPMS');
    # Copy RPMs from CSM distribution CD-ROM or directory to dirctory structure
    foreach $rpm (@::csm_packages, @::rsct_packages, @::director_packages)
    {
          if ($::PREREQS_ATTR{'PkgArchitecture'} =~ /i?86/)
          {
	    # PTF: (09/09/2002)
	    # we take out the dash because the images, may not have them.

	    $rpmfile = "${rpm}*.i?86.rpm";
	    $path = InstallUtils->findRpmInPath($rpmfile, @::PKGPATH);
          }
          else
          {
	    $rpmfile = "${rpm}*.ia64.rpm";
	    $path = InstallUtils->findRpmInPath($rpmfile, @::PKGPATH);
            if (!($path))
            {
              $rpmfile = "${rpm}*.i?86.rpm";
	      $path = InstallUtils->findRpmInPath($rpmfile, @::PKGPATH);
            }
          }
          if (!($path))
          {
	     $rpmfile = "${rpm}*.noarch.rpm";
	     $path = InstallUtils->findRpmInPath($rpmfile, @::PKGPATH);
          }

	  if (defined($path)) 
	  { 
	      # Get the latest version of the file in the first location found
	      #my $rpmfile = NodeUtils->runcmd("$::LS -vr $path/${rpm}* | $::HEAD -1", -1);
              $rpmfile = InstallUtils->getRecentVersionRpm($path, $rpmfile);
	      chomp $rpmfile;
	      my $rpmfile_basename;
	      ($rpmfile_basename = $rpmfile) =~ s:^.*/::g;

	      &copyRpm($rpmfile_basename, $path, $::INSTALLDIR_CSMPKG); 
	  }
	  else 
	  {
	      NodeUtils->message('E1', 'EMsgCANT_FIND', $rpm, join(':',@::PKGPATH));
	  }
    }
 } 
 elsif ($::PREREQS_ATTR{'OSName'} eq "AIX")
 {
    NodeUtils->message("I", 'IMsgCOPYING_INSTALLP');
    # Copy RPMs from CSM distribution CD-ROM or directory to dirctory structure
    foreach my $image (@::csm_packages){
        my $file = "${image}";
        my @images = InstallUtils->findImagesInPath($file, @::PKGPATH);
        foreach my $image (@images){
            my $basename;              
            ($basename = $image) =~ s:^.*/::g;
            my $path;
            ($path = $image) =~ s/$basename//g;
            &copyRpm($basename, $path, $::INSTALLDIR_CSMINSTLP);
        }
    }

    `inutoc $::INSTALLDIR_CSMINSTLP`;
 }
 else
 {
     NodeUtils->message('E2', 'EMsgINVALID_OSTYPE');
 }
}

# Copy the CSM requisite packages to $::INSTALLDIR_CSMPKG 
# (i.e. /csminstall/Linux/RedHat/csm/1.2.0/packages)
sub copy_opensrc_prereqs
{
    NodeUtils->message('I', 'IMsgCOPYING_OPENSRC_PREREQS');

    # Append /reqs to each PKGPATH element to search for tarballs
    my (@reqs_PKGPATH) = @::PKGPATH;
    map {s/$/\/reqs/} @reqs_PKGPATH;
  if ($::PREREQS_ATTR{'OSName'} eq "Linux")
  {
    my ($rpm_pre_file, $rpm_pre, $path);
    # Copy open source prereqs from CSM distribution CD-ROM or directory structure
    foreach $rpm_pre (@::opensrc_rpm_prereqs)
    {
      # check for the i*86 rpm in path and path/reqs....
      if ($::PREREQS_ATTR{'PkgArchitecture'} =~ /i?86/)
      {
         $rpm_pre_file = "${rpm_pre}*.i?86.rpm";
         $path = InstallUtils->findRpmInPath($rpm_pre_file, @::PKGPATH);
      }
      else
      {
         $rpm_pre_file = "${rpm_pre}*.ia64.rpm";
         $path = InstallUtils->findRpmInPath($rpm_pre_file, @::PKGPATH);
         if (!($path))
         {
            $rpm_pre_file = "${rpm_pre}*.i?86.rpm";
            $path = InstallUtils->findRpmInPath($rpm_pre_file, @::PKGPATH);
         }
      }
      if (!($path))
      {
        $path = InstallUtils->findRpmInPath($rpm_pre_file, @reqs_PKGPATH);
      }
      #...If i*86 not found in path then check for the noarch rpm ....
      if (!($path))
      {
          $rpm_pre_file = "${rpm_pre}*.noarch.rpm";
          $path = InstallUtils->findRpmInPath($rpm_pre_file, @::PKGPATH);
      }
      if (!($path))
      {
        $path = InstallUtils->findRpmInPath($rpm_pre_file, @reqs_PKGPATH);
      }
      # And finally exit if the rpm is not found else copy
      if (defined($path))
      {
          # Get the latest version of the file in the first location found
          #my $rpm_pre_file = NodeUtils->runcmd("$::LS -vr $path/$rpm_pre_file | $::HEAD -1", -1);
          $rpm_pre_file = InstallUtils->getRecentVersionRpm($path, $rpm_pre_file);
          chomp $rpm_pre_file;
          my $rpmfile_basename;
          ($rpmfile_basename = $rpm_pre_file) =~ s:^.*/::g;

          &copyRpm($rpmfile_basename, $path, $::INSTALLDIR_CSMPKG);
       }
       else
       {
            NodeUtils->message('E1', 'EMsgCANT_FIND', $rpm_pre, join(':',@::PKGPATH));
       }

    }
  }
  elsif ($::PREREQS_ATTR{'OSName'} eq "AIX")
  {
    # Copy open source prereqs from CSM distribution CD-ROM or directory structure
    foreach my $rpm_pre (@::opensrc_rpm_prereqs)
    {
      # check for the ppc rpm in path and path/reqs....
      my $rpm_pre_file = "${rpm_pre}*.ppc.rpm";
      my $path = InstallUtils->findRpmInPath($rpm_pre_file, @::PKGPATH);
      if (!($path))
      {
        $path = InstallUtils->findRpmInPath($rpm_pre_file, @reqs_PKGPATH);
      }
      #...If ppc not found in path then check for the noarch rpm ....
      if (!($path))
      {
          $rpm_pre_file = "${rpm_pre}*.noarch.rpm";
          $path = InstallUtils->findRpmInPath($rpm_pre_file, @::PKGPATH);
      }
      if (!($path))
      {
        $path = InstallUtils->findRpmInPath($rpm_pre_file, @reqs_PKGPATH);
      }
      # And finally exit if the rpm is not found else copy
      if (defined($path))
      {
          # Get the latest version of the file in the first location found
          #my $rpmfile = NodeUtils->runcmd("$::LS $path/$rpm_pre_file | $::HEAD -1", -1);
          my  $rpmfile = InstallUtils->getRecentVersionRpm($path, $rpm_pre_file);
          chomp $rpmfile;
          my $rpmfile_basename;
          ($rpmfile_basename = $rpmfile) =~ s:^.*/::g;

          &copyRpm($rpmfile_basename, $path, $::INSTALLDIR_CSMRPM);
       }
       else
       {
            NodeUtils->message('E1', 'EMsgCANT_FIND', $rpm_pre, join(':',@::PKGPATH));
       }

    }
  }
  else
  {
     NodeUtils->message('E2', 'EMsgINVALID_OSTYPE');
  }
}

#Copy the csm command  binaries...
sub copy_csmbin
{
    NodeUtils->message('I', 'IMsgCOPYING_CSMBINARIES');
 if ($::PREREQS_ATTR{'OSName'} eq "Linux")
 {
    # Files to copy to /csminstall/csm
    foreach my $bin_file (@::csm_bin_file_csm)
    {
          my @temper_val=();
          my $temper_file=();
          @temper_val=split(/\//,$bin_file);
          $temper_file= $temper_val[$#temper_val];
          $temper_file = $::INSTALLDIR_CSM."\/".$temper_file;
          if (-e $temper_file)
          {
             &run_cmd("$::RM $temper_file");
             &run_cmd("$::COPY $bin_file $::INSTALLDIR_CSM");
            #If there are any errors during the copy, run_cmd() exits.
          }
          else
          {
             &run_cmd("$::COPY $bin_file $::INSTALLDIR_CSM");
            #If there are any errors during the copy, run_cmd() exits.
          }
    }

    # Files to copy to /tftpboot
    foreach my $bin_file (@::csm_bin_file_tftpboot)
    {
          &run_cmd("$::COPY $bin_file /tftpboot");
          #If there are any errors during the copy, run_cmd() exits.
    }

    # Files to copy to /csminstall/csm/<csm_version>
    foreach my $bin_file (@::csm_bin_file_csmver)
    {
          &run_cmd("$::COPY $bin_file $::INSTALLDIR_CSMVR");
          #If there are any errors during the copy, run_cmd() exits.
    }

 }
 elsif ($::PREREQS_ATTR{'OSName'} eq "AIX")
 {
    # Files to copy to /csminstall/csm
    foreach my $bin_file (@::csm_bin_file_csm)
    {
          my @temper_val=();
          my $temper_file=();
          @temper_val=split(/\//,$bin_file);
          $temper_file= $temper_val[$#temper_val];
          $temper_file = $::INSTALLDIR_CSM."\/".$temper_file;
          if (-e $temper_file)
          {
             &run_cmd("$::RM $temper_file");
             &run_cmd("$::COPY $bin_file $::INSTALLDIR_CSM");
             #If there are any errors during the copy, run_cmd() exits.
          }
          else
          {
             &run_cmd("$::COPY $bin_file $::INSTALLDIR_CSM");
             #If there are any errors during the copy, run_cmd() exits.
          }
    }

    # Files to copy to /csminstall/csm/<csm_version>
    foreach my $bin_file (@::csm_bin_file_csmver)
    {
          &run_cmd("$::COPY $bin_file $::INSTALLDIR_CSMVR");
          #If there are any errors during the copy, run_cmd() exits.
    }


 }
 else
 {
     NodeUtils->message('E2', 'EMsgINVALID_OSTYPE');
 }

}

# Copy all the required RPM files from the RedHat CD-ROMs 
# or from the PKGPATH to /tftpboot/<distro>/RedHat/RPMS
sub copy_distro_rpms
{
 if ($::PREREQS_ATTR{'OSName'} eq "Linux")
 {
    my $distro		= $::DISTRO;
    my $destination	= $::INSTALLDIR_OS_NAME_RPMS;
    my ($disk, $cmd, $rc);
    my @notfound;
    my ($my_prereq, $prereq, $path, $dir);
	
    NodeUtils->message('I', 'IMsgCOPYING_LINUX_RPMS');


    # First try to get the distro rpms out of the pkg path (which may 
    # include the CSM CD).  This allows us to put newer versions of some 
    # distro rpms on our media if we need to.
    # Note:  DISTRO_PREREQS is a hash of array references, thus the 
    #        funny syntax below.
    my (@DISTRO_PKGPATH) = @::PKGPATH;
    map {s|$|/$::DISTRO_RPMDIR{$distro}|} @DISTRO_PKGPATH;
    
    foreach $prereq (@::os_prereq)
      {
            if ($::PREREQS_ATTR{'PkgArchitecture'} =~ /i?86/)
            {
               $my_prereq="$prereq.i?86.rpm";
	       NodeUtils->message('V', 'IMsgSEARCHING_FOR_PREREQ', $prereq);
	       $path = InstallUtils->findRpmInPath($my_prereq, @DISTRO_PKGPATH);
            }
            else
            {
               $my_prereq="$prereq.ia64.rpm";
	       NodeUtils->message('V', 'IMsgSEARCHING_FOR_PREREQ', $prereq);
	       $path = InstallUtils->findRpmInPath($my_prereq, @DISTRO_PKGPATH);
               if (!($path))
               {
                 $my_prereq="$prereq.i?86.rpm";
	         NodeUtils->message('V', 'IMsgSEARCHING_FOR_PREREQ', $prereq);
	         $path = InstallUtils->findRpmInPath($my_prereq, @DISTRO_PKGPATH);
               }
            }
            if (!($path))
            {
              $my_prereq="$prereq.noarch.rpm";
	      $path = InstallUtils->findRpmInPath($my_prereq, @DISTRO_PKGPATH);
            } 
	    if (defined($path)) 
            {
                # Get the latest version of the file in the first location found
                #my $rpm_pre_file = NodeUtils->runcmd("$::LS -vr $path/$my_prereq | $::HEAD -1", -1);
                my $rpm_pre_file = InstallUtils->getRecentVersionRpm($path, $my_prereq);
                chomp $rpm_pre_file;
                my $rpmfile_basename;
                ($rpmfile_basename = $rpm_pre_file) =~ s:^.*/::g;

                &copyRpm($rpmfile_basename, $path, $destination); 
            }
	    else { push @notfound, $prereq; }
      }

    if (!scalar(@notfound)) { return; }     # found them all

    # Copy RPMs from Redhat/TurboLinux distribution CD-ROM(s) 
    # to /tftpboot/<distro_rpmdir> (i.e. /tftpboot/rh71)
    $cmd = "umount $::MNTCDROM > /dev/null 2>&1";
    &run_cmd($cmd, "W", "ignore_errors");

    foreach $disk (split (/\s/, $::DISTRO_DISKLIST{$distro}))
    {
	NodeUtils->message('I', 'IMsgINSERT_DISK', $::DISTRO_NAME{$distro}, $disk);
	NodeUtils->message('P', 'IMsgPRESS_ENTER');

	my $junk = getc;

	$cmd = "mount $::MNTCDROM";
	&run_cmd($cmd, "W");
	foreach $prereq (@notfound)
	  {
                if ($::PREREQS_ATTR{'PkgArchitecture'} =~ /i?86/)
                { 
                  $my_prereq="$prereq.i?86.rpm";
		  if (!length($prereq)) { next; }    # already found that one
		  NodeUtils->message('V', 'IMsgSEARCHING_FOR_PREREQ_ON_CD', $prereq, $disk);
		  $dir = "$::MNTCDROM/$::DISTRO_RPMDIR{$distro}";
		  $path = InstallUtils->findRpmInPath($my_prereq, $dir);
                }
                else
                {
                  $my_prereq="$prereq.ia64.rpm";
		  if (!length($prereq)) { next; }    # already found that one
		  NodeUtils->message('V', 'IMsgSEARCHING_FOR_PREREQ_ON_CD', $prereq, $disk);
		  $dir = "$::MNTCDROM/$::DISTRO_RPMDIR{$distro}";
		  $path = InstallUtils->findRpmInPath($my_prereq, $dir);
                  if (!($path))
                  {
                    $my_prereq="$prereq.i?86.rpm";
		    if (!length($prereq)) { next; }    # already found that one
		    NodeUtils->message('V', 'IMsgSEARCHING_FOR_PREREQ_ON_CD', $prereq, $disk);
		    $path = InstallUtils->findRpmInPath($my_prereq, $dir);
                  }
                }
                if (!($path))
                {
                  $my_prereq="$prereq.noarch.rpm";
		  $path = InstallUtils->findRpmInPath($my_prereq, $dir);
                }
		if (defined($path))
		  {
                        # Get the latest version of the file in the first location found
                        #my $rpm_pre_file = NodeUtils->runcmd("$::LS -vr $path/$my_prereq | $::HEAD -1", -1);
                        my $rpm_pre_file = InstallUtils->getRecentVersionRpm($path, $my_prereq);
                        chomp $rpm_pre_file;
                        my $rpmfile_basename;
                        ($rpmfile_basename = $rpm_pre_file) =~ s:^.*/::g;

			&copyRpm($rpmfile_basename, $path, $destination);
			$prereq = '';         # so we do not search for it on the next cd
		  }
	  }

	# See if there were any we did not find
	my $nf = join(' ', @notfound);      # this eliminates the ones we blanked out
	@notfound = split(' ', $nf);
	if (!scalar(@notfound)) { last; }    # do not need to go to the next cd
	
	#$cmd = "$::COPY --update $::MNTCDROM/$::DISTRO_RPMDIR{$distro}/* $destination";
	#&run_cmd($cmd, "W");

	$cmd = "umount $::MNTCDROM > /dev/null 2>&1";
	&run_cmd($cmd, "W");
    }
	
	if (scalar(@notfound)) { NodeUtils->message('E1', 'EMsgPREREQ_NOT_FOUND', join(', ',@notfound)); }
 }
 elsif ($::PREREQS_ATTR{'OSName'} eq "AIX")
 {
 }
  
}

sub create_directory_structure
{
    mkpath ($::INSTALLDIR_CSMVRBIN, $::VERBOSE, 0755);
    mkpath ($::INSTALLDIR_CSMBIN, $::VERBOSE, 0755);
    if ($::PREREQS_ATTR{'OSName'} eq "AIX")
    {
       mkpath ($::INSTALLDIR_CSMRPM, $::VERBOSE, 0755);
       mkpath ($::INSTALLDIR_CSMINSTLP, $::VERBOSE, 0755);
    }
    elsif ($::PREREQS_ATTR{'OSName'} eq "Linux")
    {
       mkpath ($::INSTALLDIR_CSMPKG, $::VERBOSE, 0755);
       mkpath ($::INSTALLDIR_OS_NAME_RPMS, $::VERBOSE, 0755);
       #mkpath ($::INSTALLDIR_OS_NAME_UPDATES, $::VERBOSE, 0755);
       #make updates and RPMS directories
       mkpath ($::SMS_UPDATES, $::VERBOSE, 0755);
       #we made need to add different symbolic links for SUSE, etc.
       if($::PREREQS_ATTR{'DistributionName'} eq "RedHat"){
       	       #this is a symbolic link to ->RedHat/RPMS
       	       if(!(-e "$::SMS_RPMS")){
       	       		my $ln_cmd = "$::LN -s $::INSTALLDIR_OS_NAME_RPMS $::SMS_RPMS";
       	       		&run_cmd($ln_cmd, "W", "show_all");
	       }
       }
       else{ 
       		mkpath ($::SMS_RPMS, $::VERBOSE, 0755);
       }
       if ( (-f "/tftpboot") )
       {
            
         NodeUtils->message('E2', 'EMsgNo_Directory',"\/tftpboot");
       }
       else
       {
           mkpath ("/tftpboot", $::VERBOSE, 0755);
       }
    }
    else
    {
         NodeUtils->message('E2', 'EMsgINVALID_OSTYPE');
    }
}

# 
# install_rpm
# 
# Install a single RPM.
# Arguments:
#      rpm name (ie csm.core)
#      flags for the rpm command (ie --nodeps)
#      installflag (-U by default) - whether to upgrade(-U) or install(-i)
# Returns:
#      0  if installed sucessfully or already installed
#      >0 if the rpm failed to install
#
sub install_rpm
{
    my ($cmd, $rc, @cmdout, $cmdout, $returncode, @my_path);
    my ($rpmfile, $path, $rpmfile_basename, $installflag);
    my ($rpm, $flags, $installflag) = @_;
    if (! $installflag) { $installflag = "-U"; }	# Default to -U
    if ($::PREREQS_ATTR{'OSName'} eq "Linux")
    {
       @my_path=($::INSTALLDIR_CSMPKG, $::INSTALLDIR_OS_NAME_RPMS);
    }
    elsif ($::PREREQS_ATTR{'OSName'} eq "AIX")
    {
       @my_path=($::INSTALLDIR_CSMRPM, $::INSTALLDIR_CSMINSTLP);
    }
    else
    {
       NodeUtils->message('E2', 'EMsgINVALID_OSTYPE');
    }
    #my $rpmfile = InstallUtils->findRpm($rpm,undef,undef,@my_path);

    if ($::PREREQS_ATTR{'OSName'} eq "Linux")
    {
       if ($::PREREQS_ATTR{'PkgArchitecture'} =~ /i?86/)
       {
         $rpmfile = "${rpm}*.i?86.rpm";
         $path = InstallUtils->findRpmInPath($rpmfile, @my_path);
       }
       else
       {
         $rpmfile = "${rpm}*.ia64.rpm";
         $path = InstallUtils->findRpmInPath($rpmfile, @my_path);
         if (!($path))
         {
           $rpmfile = "${rpm}*.i?86.rpm";
           $path = InstallUtils->findRpmInPath($rpmfile, @my_path);
          }
       }
       if (!($path))
       {
          $rpmfile = "${rpm}*.noarch.rpm";
          $path = InstallUtils->findRpmInPath($rpmfile, @my_path);
       }

       if (defined($path))
       {
         # Get the latest version of the file in the first location found
         $rpmfile = InstallUtils->getRecentVersionRpm($path, $rpmfile);
         chomp $rpmfile;
         $rpmfile_basename;
         ($rpmfile_basename = $rpmfile) =~ s:^.*/::g;
      }
      else
      {
        NodeUtils->message('E1', 'EMsgCANT_FIND', $rpm, join(':',@my_path));
      }
    }
    elsif ($::PREREQS_ATTR{'OSName'} eq "AIX")
    {
      # check for the ppc rpm in path and path/reqs....
      $rpmfile = "${rpm}*.ppc.rpm";
      $path = InstallUtils->findRpmInPath($rpmfile, @my_path);
      if (!($path))
      {
          $rpmfile = "${rpm}*.noarch.rpm";
          $path = InstallUtils->findRpmInPath($rpmfile, @my_path);
      }
      if (defined($path))
      {
          # Get the latest version of the file in the first location found
          $rpmfile = InstallUtils->getRecentVersionRpm($path, $rpmfile);
          chomp $rpmfile;
          $rpmfile_basename;
          ($rpmfile_basename = $rpmfile) =~ s:^.*/::g;
       }
       else
       {
            NodeUtils->message('E1', 'EMsgCANT_FIND', $rpm, join(':',@::PKGPATH));
       }


    }

    $returncode = 0;

    # Check if the RPM is already installed at the correct version
    $cmd = "$::RPMCMD -U --test --nodeps $rpmfile 2>&1";
    NodeUtils->message('V', 'IMsgCMD', $cmd);
    @cmdout = `$cmd`;
    $rc = $? >> 8;	
    $cmdout = join('', @cmdout); 	# Put output into a single string
    # Don't print a message on command failure here.
    # The return code is > 0 if the RPM is not installed.
#    if ($rc) { 
#	NodeUtils->message("E$rc",'EMsgCMD_FAILED',$cmd,$cmdout); 
#    }

    # if the force flag is on then the variable cmdout should be nothing
    if ($::FORCE_INSTALL)
    {
      $cmdout="";
    }
    #if (($cmdout =~ /package $rpm is not installed/) || $::FORCE_INSTALL)
    if ($cmdout =~ /package $rpm.* is already installed/)
    {
	# RPM is already installed.
	
	NodeUtils->message('I', 'IMsgRPM_ALREADY_INSTALLED', $rpm);
	$rc = 0;
    }
    elsif (($cmdout !~ /package $rpm.* is already installed/) || $::FORCE_INSTALL)
    {
	# If this is one of the GPL open source packages, display a license
	# agreement.  Don't display the license again if it has already been 
	# displayed.
	if (grep(/^$rpm$/, @::GPL_PACKAGES) and ! $::DISPLAYED_GPL_LICENSE)
	{
	    NodeUtils->message('I', 'IMsgGPL_LICENSE');
	    NodeUtils->message('P', 'IMsgPRESS_ENTER_TO_INSTALL');
	    my $t = <STDIN>; chop($t);
	    if (length($t) > 0)
	    {
		NodeUtils->message('I', 'IMsgCSM_NOT_INSTALLING');
		exit 1;
	    }

	    # Set to 1 so the license is not displayed again.
	    $::DISPLAYED_GPL_LICENSE = 1;
	}

	# RPM is not installed, so install its dependencies and then it
	#print "calling installRequiredRpms() for $rpmfile\n";
	InstallUtils->installRequiredRpms($rpmfile, @my_path);
	if($::ENGLISH){
		NodeUtils->message('I', 'IMsgINSTALLING_RPM', $rpmfile_basename);
	}
	else{ #workaround for NLS problem
		NodeUtils->message('I', 'IMsgINSTALLING_RPMS', $rpmfile_basename);
	}
	$cmd = "$::RPMCMD $installflag $flags $rpmfile";
	$cmd = "$cmd --force" if $::FORCE_INSTALL;
	$rc = &run_cmd($cmd, "W", "show_all");
    }
    else
    {
	$rc = 2;
	NodeUtils->message('E$rc','EMsgCMD_FAILED',$cmd,$cmdout);
    }

    $returncode = $rc;
    return ($returncode);
}

sub install_installp_packages
{
    my ($cmd, $rc, @cmdout, $cmdout, $returncode, @my_path);
    $returncode = 0;
    my ($pkg) = @_;
    unless($pkg =~ /msg/){
        $pkg =~ s/\*//;
        if($::ENGLISH){
        	NodeUtils->message('I', 'IMsgINSTALLING_RPM', $pkg);
	}
	else{ #NLS workaround
		NodeUtils->message('I', 'IMsgINSTALLING_INSTALLP_PACKAGES', $pkg);
	}
        $cmd = "$::INSTALLPCMD -a -d $::INSTALLDIR_CSMINSTLP -X $pkg 2>&1";
        if ($::FORCE_INSTALL)
        {
          $cmd = "$::INSTALLPCMD -a -d $::INSTALLDIR_CSMINSTLP ";
          $cmd .= "-X -F $pkg 2>&1";
        }
        $rc = &run_cmd($cmd, "W", "show_all");
        $returncode = $rc;
    }else{
     	chomp(my @msgfiles = `/bin/ls $::INSTALLDIR_CSMINSTLP/$pkg`);
	foreach my $msgFile (@msgfiles){
            # get rid of numbers at the end
            $msgFile =~ s/(.*)(csm\.msg\.[A-Za-z]{2}_[A-Za-z]{2})(.*)/$2/;
            if($::ENGLISH){
            	NodeUtils->message('I', 'IMsgINSTALLING_RPM', $msgFile);
	    }
	    else{ #NLS workaround
		    NodeUtils->message('I', 'IMsgINSTALLING_INSTALLP_PACKAGES', $msgFile);
	    }
            $cmd = "$::INSTALLPCMD -a -d $::INSTALLDIR_CSMINSTLP -X ";
            $cmd .= "$msgFile 2>&1";
            if ($::FORCE_INSTALL){
                $cmd = "$::INSTALLPCMD -a -d $::INSTALLDIR_CSMINSTLP ";
                $cmd .= "-X -F $msgFile 2>&1";
            }
            $rc = &run_cmd($cmd, "W", "show_all");
            if ($rc > $returncode){
                $returncode = $rc;
            }
        }
    }
    return ($returncode);
}

# Special case for atftp because its requires don't work on RedHat 7.1 
# and tftp-server must be uninstalled first.  This subroutine first removes 
# tftp-server if it is already installed.  Then, it attempts to install atftp 
# normally. If it fails, try installing it again with the --nodeps flag.
sub install_atftp
{
    my ($cmd, $output, $rc, $returncode);
    $returncode=0;

    # Remove tftp-server if it exists, because it conflicts with atftp.
    $cmd = "$::RPMCMD -q tftp-server";
    $output = NodeUtils->runcmd($cmd, -1);
    if ($::RUNCMD_RC == 0)
    { 	# tftp-server is already installed, remove it.
	NodeUtils->message("I", 'IMsgUNINSTALLING_TFTP');
	$cmd = "$::RPMCMD -e tftp-server";
	$output = NodeUtils->runcmd($cmd, 0);
	$returncode = $::RUNCMD_RC if ($::RUNCMD_RC > $returncode);
    }

    if ($returncode == 0)
    {
	# Install atftp
	$rc = &install_rpm('atftp');
	if ($rc)
	{
	    # If that didn't work, try to install atftp again, 
	    # this time with the --nodeps flag.
	    # atftp doesn't install properly on RedHat 7.1 because of a bad
	    # readline dependency, so we get around it by ignoring the 
	    # dependencies.
	    NodeUtils->message("I", 'IMsgINSTALLING_ATFTP_RETRY');
	    $rc = &install_rpm('atftp', '--nodeps');
	    $returncode = $rc if ($rc > $returncode);
	}

	$rc = InstallUtils->setup_atftp();
	$returncode = $rc if ($rc > $returncode);
    }

    return ($returncode);

}

# Install all the RPMs for Linux and installp images for Aix
sub install_packages
{
    my ($rpm, $tarball_list, $cmd, $rc, $returncode);
    my @linux_prereqs;
  if ($::PREREQS_ATTR{'OSName'} eq "Linux")
  {
    $rc = 0;
    $returncode = 0;

    NodeUtils->message("I", 'IMsgINSTALLING_RPMS', "OPEN SOURCE PRE REQUISITIES");
    
    # This is the list of GPL open source packages to be installed.  If any
    # of these packages must be installed, a license is displayed first
    # and the user must press Enter to accept the license.
    @::GPL_PACKAGES = ('atftp', 'syslinux');

    # This variable is set when the GPL license has been displayed once,
    # and prevents the license question from being displayed again.
    $::DISPLAYED_GPL_LICENSE = 0;

    foreach $rpm (@::server_open_srcrpms)
    {
	if ($rpm eq "atftp")
	{
	    # Special case for atftp because its requires don't work 
	    # on RedHat 7.1 and tftp must be uninstalled first.
	    $rc = &install_atftp();	
	    $returncode = $rc if ($rc > $returncode);
	}
	elsif ($rpm =~ /^IBMJava2-JRE/)
        {
            # if IBMJava2-SDK is already installed on the system then 
	    # don't install IBMJava2-JRE rpm.  
	    # Note that the $rpm variable contains the JRE version number
	    # at the end.  This ensures that we always use the correct version.
	    my $SDK_rpm;
	    ($SDK_rpm = $rpm) =~ s/IBMJava2-JRE/IBMJava2-SDK/;
	    #print "RPM = $rpm, SDK_rpm = $SDK_rpm\n";
            NodeUtils->runcmd("$::RPMCMD -q $SDK_rpm", -1);
            if ($::RUNCMD_RC !=0)   
            {
	       # Install the IBMJava2-JRE package with -i instead of -U
	       # so that multiple java versions can be installed at the same
	       # time.  Also, use the --oldpackage flag to allow an older
	       # version of java to be installed even if a newer version is
	       # already installed.
	       $rc = &install_rpm($rpm, "--oldpackage", "-i");
	       $returncode = $rc if ($rc > $returncode);
            }
        }
        else
	{
	    $rc = &install_rpm($rpm);
	    $returncode = $rc if ($rc > $returncode);
	}
    }

    NodeUtils->message('I', 'IMsgINSTALLING_RPMS', "RSCT");
    foreach $rpm (@::rsct_server_packages)
    {
	$rc = &install_rpm($rpm);
	$returncode = $rc if ($rc > $returncode);
    }
    # if the force option is set, the run the rsct configure and unconfigure scripts
    if ($::FORCE_INSTALL)
    {
      &run_cmd("/usr/sbin/rsct/install/bin/uncfgct");
      &run_cmd("/usr/sbin/rsct/install/bin/cfgct");
      &run_cmd("/usr/sbin/rsct/bin/rmcctrl -p");
    }

	# TO DO: if rsct.core was already installed, but ctrmc was not started, 
	# this would have to time out before continuing
	# &waitForERRM();  -- this is now done in csmserverposti

    NodeUtils->message('I', 'IMsgINSTALLING_RPMS', "CSM");
    foreach $rpm (@::csm_server_packages)
    {
	$rc = &install_rpm($rpm);
	$returncode = $rc if ($rc > $returncode);
    }
    return $returncode;
  }
  elsif ($::PREREQS_ATTR{'OSName'} eq "AIX")
  {
    $rc = 0;
    $returncode = 0;

    NodeUtils->message('I', 'IMsgINSTALLING_RPMS', "OPEN SOURCE PRE REQUISITIES");
    foreach $rpm (@::server_open_srcrpms)
    {
	if($rpm =~ /^openCIMOM/){
	       $rc = &install_rpm($rpm, "--oldpackage");
	       $returncode = $rc if ($rc > $returncode);
	}
	else{ #do a regular rpm install
		$rc = &install_rpm($rpm);
		$returncode = $rc if ($rc > $returncode);
	}
    }

    NodeUtils->message('I', 'IMsgINSTALLING_INSTALLP_PACKAGES', "CSM");
    foreach $rpm (@::csm_server_packages)
    {
	$rc = &install_installp_packages($rpm);
	$returncode = $rc if ($rc > $returncode);
    }
    return $returncode;
  }
}

sub checkrpm
{
  my $ret_code;
  if ($::PREREQS_ATTR{'OSName'} eq "AIX")
  {
    $ret_code=InstallUtils->get_CSMVersion("rpm\.rte");
     if (length($ret_code)==0)
     {
       NodeUtils->message('E2', 'EMsgNO_RPM');
     } 
  }
  elsif ($::PREREQS_ATTR{'OSName'} eq "Linux")
  {
    $ret_code=InstallUtils->get_CSMVersion("rpm");
     if (length($ret_code)==0)
     {
       NodeUtils->message('E2', 'EMsgNO_RPM');
     } 
  }
  else
  {
     NodeUtils->mesage('E2', 'EMsgINVALID_OSTYPE');
  }
}

#
# Set global cluster attributes by using ATTR=Value provided by the user.
# This set attributes in the dmsctrl resource class.
# The attributes are stored in the %::ATTRS hash.
#
sub set_cluster_attrs
{
    my $rc = 0;
    my ($attr, $value, $dmsctrlkey);

    return 0 if (! (keys %::ATTRS));

    my $cmd = "/opt/csm/bin/csmconfig";

    foreach $attr (keys %::ATTRS)
    {
	$value = $::ATTRS{$attr};
	#print "ATTR = $attr, VALUE = $value\n";

	$cmd .= " $attr=$value"

	#my $rmccmd = "-s IBM.DmsCtrl::::" . $attr . "::" . $value;
	#my $outref = NodeUtils->runrmccmd('chrsrc-api', '-i', $rmccmd);

	#my ($unrecognized, $maxnodes) = split(/:\|:/, $$outref[0]);
	#print "UNRECOGNIZED = $unrecognized\n";
	#print "MAXNODES = $maxnodes\n";
    }

    my $output = NodeUtils->runcmd($cmd, 0);
    $rc = $::RUNCMD_RC;

    return $rc;
}

#
# Install the IBM Director Agent and Linux Cluster Support Extension.
# Only install if -D was provided
sub install_director
{
    my ($rc) = 0;

    if ($::INSTALL_DIRECTOR)
    {
	NodeUtils->message('V', 'IMsgINSTALLING_DIRECTOR');
	my $cmd = "$::ADDLCSEXT -p $::INSTALLDIR_CSMPKG";
	my $output = NodeUtils->runcmd($cmd, 0);
	$rc = $::RUNCMD_RC;

    }
    return $rc;
}

#---------------------
# Main
#---------------------
my ($rc) = 0;
my ($returncode) = 0;

&getArgs;

# start logging
InstallUtils->start_logging($::INSTALLMSLOG);

#setting and checking the variables
&set_variables;

# Check rpm package is installed 
&checkrpm;
&create_directory_structure;

# Check if DONT_COPY_RPMS  i.e., -x flag is set if not set then copy rpms/images
if ( ! $::DONT_COPY_RPMS )
{
   # Copy all the packages to directory structure 
   &copy_csm;
   &copy_opensrc_prereqs;
   &copy_distro_rpms;		
}

# Verify current installation
&checkPreviousInstallation;	# Does nothing for now


# Install the packages.
# Install rpms for Linux or installp images for AIX.
$rc = &install_packages;	
$returncode = $rc if ($rc > $returncode);

# Set global cluster attributes in the dmsctrl resource class
$rc = &set_cluster_attrs;
$returncode = $rc if ($rc > $returncode);

# Install the Director Extensions if requested
if ($::INSTALL_DIRECTOR)
{
    &install_director;
}

#copy the csm command binaries
&copy_csmbin;

# start logging
InstallUtils->stop_logging();

exit $returncode;
